package com.fesine.learn.dao.impl;

import com.fesine.learn.dao.RedisDao;
import com.fesine.learn.dao.UserInfoDao;
import com.fesine.learn.po.UserInfoPo;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;

import java.io.*;

/**
 * Created by Administrator on 2017/4/24.
 */
@Repository
public class UserInfoDaoImpl extends RedisDao implements UserInfoDao {


    /**
     * redis新增
     * @param u
     * @return
     */
    public boolean add(final UserInfoPo u) {
        /** 利用fastjson将po进行序列化 **/
//        final String userInfo=JSONArray.toJSON(u).toString();
        boolean result=(Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                RedisSerializer<String> serializer=getRedisSerializer();
                byte[] id=serializer.serialize(u.getId());
                return redisConnection.setNX(id,serialize(u));
            }
        });
        return result;
    }

    /**
     * 根据key查询userInfo
     */
    public UserInfoPo find(final String id){
        UserInfoPo result=(UserInfoPo) redisTemplate.execute(new RedisCallback() {
            public UserInfoPo doInRedis(RedisConnection redisConnection) throws DataAccessException {
                RedisSerializer<String> serializer=getRedisSerializer();
                byte[] key=serializer.serialize(id);
                byte[] userInfoSe=redisConnection.get(key);
                /** 利用fastjson将String反序列化为实体类**/
//                String us=new String(userInfoSe);
//                UserInfoPo u = JSON.parseObject(us,UserInfoPo.class);
                UserInfoPo u=unSerializ(userInfoSe);
                return u;
            }
        });
        return result;
    }

    /**
     * 根据key删除userInfo
     * @param id
     */
    public void delete(String id) {
        redisTemplate.delete(id);
    }

    /**
     * 根据key修改userInfo
     * @param u
     */
    public boolean update(final UserInfoPo u) {
        boolean result=(Boolean)redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                RedisSerializer<String> serializer=getRedisSerializer();
                byte[] key=serializer.serialize(u.getId());
                redisConnection.set(key,serialize(u));
                return true;
            }
        });
        return result;
    }


    /**
     * 对象序列化
     */
    public byte[] serialize(UserInfoPo u){
        try {
            ByteArrayOutputStream bout=new ByteArrayOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(bout);
            oos.writeObject(u);
            byte[] userInfoSe=bout.toByteArray();
            oos.close();
            return userInfoSe;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对象反序列化
     */
    public UserInfoPo unSerializ(byte[] userInfoSe){
        try {
            ByteArrayInputStream bput=new ByteArrayInputStream(userInfoSe);
            ObjectInputStream ois=new ObjectInputStream(bput);
            UserInfoPo u=(UserInfoPo)ois.readObject();
            ois.close();
            return u;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
